
$(function () {

    /*调用函数*/
    getData();


    var newData = {result: []};//先声明出大的数据
    var total = 80;//总共的张数

    /*获取数据*/
    function getData() {
        $.ajax({
            type: 'get',
            url: 'http://139.199.157.195:9090/api/getinlanddiscount',
            datatype: 'json',
            success: function (info) {//获取后台result数组中的数据，并将数据翻倍
                //console.log(info);
                newData.result = info.result.concat(info.result);

                newData.result = newData.result.concat(newData.result);
                //console.log(newData.result.length);

                loaded();

                /*var html = template('tpl', newData);
                 $('.row').html(html);*/
            }
        });
    }


    function loaded() {
        //已经加载的列表个数
        /*var loadLength = $('li').length;
         console.log(loadLength);*/
        //加载完毕，禁止重复加载
        if (newData.result.length > total) {
            return;
        }

        var data = {result: []};
        var length = 8;
        if (newData.result.length <= length) {
            length = newData.result.length;
        }
        for (var i = 0; i < length; i++) {
            data.result.push(newData.result.shift());
        }

        var html = template('productId', data);

        $('ul').append(html);

    }//将图片8张，8张的追加到页面中

    /*加载数据的开关*/
    var flag = false;
    window.onscroll = function () {
        if (newData.result.length ==0||flag) {
            return;
        }

            var height = $('ul').height()+ $("#header").height() + $("#footer").height() - $(document.body).height();
            var b = height - $(document.body).scrollTop();

            if (b < 50) {

                loaded();
            }
            flag = false;
        }


});
